clear
clear matrix
set mem 80m
set matsize 800
set more off
capture log close

cd "D:\MyPapers\Happiness\TWINSDATA\code\2023May-Dataverse"
log using Table5_GLS.txt, replace
use twins_PNAS.dta, clear
keep if mz

global reshape_list "citycode d_Chengdu d_Chongqing d_Haerbin d_Hefei happy_4s happiness happy_d income income_num income_bracket IVincome age agesquared male BirthOrder_ElderDummy birth_weight VeryEarly_disease_d early_disease_d edu working work_over_40h  married divorced widowed population birth_year health edu_spouse edu_spouse_atmarriage income_spouse_atmarriage Dhappy_4s Dhappiness Dhappy_d Dincome IVDincome sDincome sIVDincome DBirthOrder_ElderDummy Dbirth_weight DVeryEarly_disease_d Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation Dhealth Dedu_spouse Dedu_spouse_atmarriage Dincome_spouse_atmarriage"

reshape wide $reshape_list, i(twinpair) j(twcode)

gen eduT=edu1+edu2
gen marriedT= married1+married2
gen divorcedT=divorced1+divorced2
gen widowedT = widowed1+widowed2
gen populationT = population1+population2

gen BirthOrder_ElderDummyT = BirthOrder_ElderDummy1+BirthOrder_ElderDummy2

gen workingT = working1+working2
gen work_over_40hT = work_over_40h1+work_over_40h2
gen birth_weightT = birth_weight1+birth_weight2
gen VeryEarly_disease_dT = VeryEarly_disease_d1+VeryEarly_disease_d2
gen early_disease_dT = early_disease_d1+early_disease_d2

constraint def 1 [happy_4s1]age1=[happy_4s2]age2
constraint def 2 [happy_4s1]agesquared1=[happy_4s2]agesquared2
constraint def 3 [happy_4s1]male1=[happy_4s2]male2
constraint def 4 [happy_4s1]d_Chengdu1=[happy_4s2]d_Chengdu2
constraint def 5 [happy_4s1]d_Chongqing1=[happy_4s2]d_Chongqing2
constraint def 6 [happy_4s1]d_Haerbin1=[happy_4s2]d_Haerbin2
constraint def 7 [happy_4s1]d_Hefei1=[happy_4s2]d_Hefei2

constraint def 8 [happy_4s1]income1=[happy_4s2]income2
constraint def 9 [happy_4s1]income2=[happy_4s2]income1

constraint def 11 [happy_4s1]edu1=[happy_4s2]edu2
constraint def 12 [happy_4s1]married1=[happy_4s2]married2
constraint def 13 [happy_4s1]eduT=[happy_4s2]eduT
constraint def 14 [happy_4s1]marriedT=[happy_4s2]marriedT

constraint def 15 [happy_4s1]BirthOrder_ElderDummy1=[happy_4s2]BirthOrder_ElderDummy2
constraint def 16 [happy_4s1]work_over_40h1=[happy_4s2]work_over_40h2
constraint def 17 [happy_4s1]birth_weight1=[happy_4s2]birth_weight2
constraint def 18 [happy_4s1]early_disease_d1=[happy_4s2]early_disease_d2
constraint def 41 [happy_4s1]VeryEarly_disease_d1=[happy_4s2]VeryEarly_disease_d2

constraint def 19 [happy_4s1]BirthOrder_ElderDummyT=[happy_4s2]BirthOrder_ElderDummyT
constraint def 20 [happy_4s1]work_over_40hT=[happy_4s2]work_over_40hT
constraint def 21 [happy_4s1]birth_weightT=[happy_4s2]birth_weightT
constraint def 22 [happy_4s1]early_disease_dT=[happy_4s2]early_disease_dT
constraint def 42 [happy_4s1]VeryEarly_disease_dT=[happy_4s2]VeryEarly_disease_dT

constraint def 26 [happy_4s1]edu2   =[happy_4s2]edu1
constraint def 27 [happy_4s1]married2   =[happy_4s2]married1 
constraint def 28 [happy_4s1]BirthOrder_ElderDummy2   =[happy_4s2]BirthOrder_ElderDummy1
constraint def 29 [happy_4s1]work_over_40h2   =[happy_4s2]work_over_40h1 

//add more marital dummies, family size, and working
constraint def 32 [happy_4s1]divorced1=[happy_4s2]divorced2
constraint def 33 [happy_4s1]divorcedT=[happy_4s2]divorcedT
constraint def 34 [happy_4s1]widowed1=[happy_4s2]widowed2
constraint def 35 [happy_4s1]widowedT=[happy_4s2]widowedT
constraint def 36 [happy_4s1]population1=[happy_4s2]population2
constraint def 37 [happy_4s1]populationT=[happy_4s2]populationT

constraint def 38 [happy_4s1]working1=[happy_4s2]working2
constraint def 39 [happy_4s1]workingT=[happy_4s2]workingT

//for perception of sibling's income: replace constrain 9 by 40
constraint def 40 [happy_4s1]IVincome2=[happy_4s2]IVincome1

sureg (happy_4s1 income1 income2 edu1 eduT working1 workingT work_over_40h1 work_over_40hT married1 marriedT divorced1 divorcedT widowed1 widowedT population1 populationT ///
       age1 agesquared1 male1 BirthOrder_ElderDummy1 BirthOrder_ElderDummyT birth_weight1 birth_weightT early_disease_d1 early_disease_dT d_Chengdu1 d_Chongqing1 d_Haerbin1 d_Hefei1) ///
      (happy_4s2 income1 income2 edu2 eduT working2 workingT work_over_40h2 work_over_40hT married2 marriedT divorced2 divorcedT widowed2 widowedT population2 populationT ///
       age2 agesquared2 male2 BirthOrder_ElderDummy2 BirthOrder_ElderDummyT birth_weight2 birth_weightT early_disease_d2 early_disease_dT d_Chengdu2 d_Chongqing2 d_Haerbin2 d_Hefei2), constr (1-9 11-22 32-39)
outreg2 using "TableS5-GLS.doc", nolabel replace coefastr  se drop(d_*)

//for perception of sibling's income: replace constrain 9 by 40, 
sureg (happy_4s1 income1 IVincome2 edu1 eduT working1 workingT work_over_40h1 work_over_40hT married1 marriedT divorced1 divorcedT widowed1 widowedT population1 populationT ///
       age1 agesquared1 male1 BirthOrder_ElderDummy1 BirthOrder_ElderDummyT birth_weight1 birth_weightT early_disease_d1 early_disease_dT d_Chengdu1 d_Chongqing1 d_Haerbin1 d_Hefei1) ///
      (happy_4s2 IVincome1 income2 edu2 eduT working2 workingT work_over_40h2 work_over_40hT married2 marriedT divorced2 divorcedT widowed2 widowedT population2 populationT ///
       age2 agesquared2 male2 BirthOrder_ElderDummy2 BirthOrder_ElderDummyT birth_weight2 birth_weightT early_disease_d2 early_disease_dT d_Chengdu2 d_Chongqing2 d_Haerbin2 d_Hefei2), constr (1-8 40 11-22 32-39)
outreg2 using "TableS5-GLS.doc", nolabel append coefastr  se drop(d_*)

log close
exit, clear
